package com.flute.icrawler.framework.autoadjust;

import com.flute.icrawler.config.CrawlConfig;
import com.flute.icrawler.framework.framework.BerkeleyCrawlUrlPool;
import com.flute.icrawler.framework.framework.CrawlUrl;

/**
 * 对于url的处理速度有限制的一个url容器
 * 
 * @author Administrator
 * 
 */
public class IntervalCrawlUrlPool extends BerkeleyCrawlUrlPool {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7394049377389136768L;
	/**
	 * 处理速度自动调整器
	 */
	private FetchAdjuster adjuster;

	public IntervalCrawlUrlPool(CrawlConfig crawlConfig) {
		super(crawlConfig);
		this.adjuster = new FetchAdjuster(new AbleTimer(100));
	}

	public IntervalCrawlUrlPool(CrawlConfig crawlConfig,
			FetchAdjuster fetchAdjuster) {
		super(crawlConfig);
		this.adjuster = fetchAdjuster;
	}

	/*
	 * 通过AbleTimer来控制网页抓取间隔
	 * 
	 * @see com.cpkf.yyjd.crawler.framework.CrawlUrlPool#nextUrl()
	 */
	public CrawlUrl nextUrl() {
		CrawlUrl url = null;
		//if (adjuster.getTimer().isAble()) {
			if (waitQueue.size() > 0) {
				url = super.nextUrl();
				adjuster.getTimer().setAble(false);
			}
		//}

		return url;
	}

	/**
	 * 监控功能接口 获取在等待中的url个数
	 */
	public int getWaitingUrlCount() {
		return waitQueue.size();
	}

	/**
	 * 监控功能接口 已经处理成功的url个数
	 */
	public int getSuccessUrlCount() {
		return sucessedQueue.size();
	}

	/**
	 * 监控功能接口 已经处理失败的url个数
	 */
	public int getFailUrlCount() {
		return failedQueue.size();
	}

	/**
	 * 监控功能接口 需要重新放入处理链条的url个数
	 */
	public int getRetryUrlCount() {
		return retriedQueue.size();
	}
}
